package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"strconv"
	"strings"
	"time"

	"github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main() {
	data := read("1405211_11.json")
	// data = append(data, read("2020-04.json")...)
	log.Println(len(data))
	// save(read("t.json"))
	save(data)
}

func read(file string) []map[string]interface{} {
	bs, _ := ioutil.ReadFile(file)
	os := []map[string]interface{}{}
	json.Unmarshal(bs, &os)

	return os
}

func save(data []map[string]interface{}) {
	f := excelize.NewFile()
	for i, title := range []string{"订单编号", "订单状态", "下单时间", "买家昵称", "收件人", "收件人电话", "收件地址", "商品信息", "金额", "重量", "买家留言", "卖家备注", "我打备注", "我打分类", "快递公司", "快递模板", "运单号", "运费", "付款时间", "打印时间", "发货时间", "发货人", "发货人电话", "发货地址", "商品图片"} {
		c, _ := excelize.ColumnNumberToName(i + 1)
		f.SetCellValue("Sheet1", c+"1", title)
	}

	for r, d := range data {
		for i := range []string{"订单编号", "订单状态", "下单时间", "买家昵称", "收件人", "收件人电话", "收件地址", "商品信息", "金额", "重量", "买家留言", "卖家备注", "我打备注", "我打分类", "快递公司", "快递模板", "运单号", "运费", "付款时间", "打印时间", "发货时间", "发货人", "发货人电话", "发货地址", "商品图片"} {
			c, _ := excelize.ColumnNumberToName(i + 1)
			var v interface{}
			switch i {
			case 0: // 订单编号
				v = toString(d["pid"])
			case 1: // 订单状态
				v = d["trade_status"]
			case 2: // 下单时间
				v = date(d["created"])
			case 3: // 买家昵称
				v = d["buyer_nick"]
			case 4: // 收件人
				v = d["receiver_name"]
			case 5: // 收件人电话
				v = d["receiver_mobile"]
			case 6: // 收件地址
				v = address(d["receiver_state"], d["receiver_city"], d["receiver_district"], d["receiver_address"])
			case 7: // 商品信息
				v = d["goods_title"]
			case 8: // 金额
				v = d["payment"]
			case 10: // 买家留言
				v = d["buyer_message"]
			case 11: // 卖家备注
				v = d["seller_memo"]
			case 12: // 我打备注
				v = d["wd_memo"]
			case 9:
				v = d["weight"]
			case 13:
				v = d["wd_flag"]
			case 15: // 重量 我打分类, 快递模板
				v = ""
			case 14: // 快递公司
				v = d["express_name"]
			case 16: // 运单号
				v = d["out_sid"]
			case 17: // 运费
				v = d["post_fee"]
			case 18:
				v = date(d["pay_time"])
			case 19: // 打印时间
				v = date(d["last_express_print_time"])
			case 20:
				v = date(d["consign_time"])
			case 21, 22, 23: // 发货人
				s, has := d["shipping_info"]
				if !has {
					continue
				}
				j, ok := s.(string)
				if !ok {
					continue
				}
				if j != "" {
					sh := map[string]interface{}{}
					json.Unmarshal([]byte(j), &sh)
					switch i {
					case 21:
						v = sh["contact"]
					case 22:
						v = sh["phone"]
					case 23:
						v = address(sh["state"], sh["city"], sh["district"], sh["address"])
					}
				}
			case 24: // 商品图片
				s, has := d["trades_json"]
				if !has {
					continue
				}
				log.Println(s)
				j, ok := s.(string)
				if !ok {
					continue
				}
				log.Println(j)
				if j != "" {
					url := []string{}
					a := []map[string]interface{}{}
					json.Unmarshal([]byte(j), &a)
					log.Println(a)
					for _, t := range a {
						orders := t["orders"].([]interface{})
						for _, order := range orders {
							o := order.(map[string]interface{})
							url = append(url, o["picPath"].(string))
						}
					}
					v = strings.Join(url, "\n")
				}

			}
			f.SetCellValue("Sheet1", fmt.Sprintf("%s%d", c, r+2), v)
		}
	}

	if err := f.SaveAs("1405211_11.xlsx"); err != nil {
		fmt.Println(err)
	}
}

func address(is ...interface{}) string {
	ret := ""

	for _, i := range is {
		if i == nil {
			continue
		}
		ret += i.(string)
	}

	return ret
}

func date(i interface{}) string {
	if i == nil {
		return ""
	}

	switch f := i.(type) {
	case float64:
		t := time.Unix(int64(f)/1e3, 0)
		return t.Format("2006-01-02 15:04:05")
	case string:
		return f
	default:
		return ""
	}
}

func toString(i interface{}) string {
	switch f := i.(type) {
	case float64:
		return strconv.FormatInt(int64(f), 10)
	case int64:
		return strconv.FormatInt(f, 10)
	case string:
		return f
	default:
		return ""
	}
}
